﻿#if WINDOWS_PHONE
#else
using Microsoft.Xna.Framework.Net;

namespace JGLib.Net
{
    public abstract class LobbyState : JGLib.States.State
    {
        protected NetworkSession NetSession { get; set; }

        protected PacketReader NetReader { get; set; }
        protected NetUtil.ReadPackets MessageReader { get; set; }
        protected PacketWriter NetWriter { get; set; }

        public LobbyState(object ID, NetworkSession session, PacketReader netReader, NetUtil.ReadPackets messageReader,
            PacketWriter netWriter)
            : base(ID)
        {
            this.NetSession = session;
            this.NetReader = netReader;
            this.MessageReader = messageReader;
            this.NetWriter = netWriter;
        }

        protected override void Update(double gameTime)
        {
            LocalNetworkGamer localGamer = this.NetSession.LocalGamers[0];

            if (this.AllGamersReady())
            {
                this.SendStartGameMessage(this.GetStartGameMessage(), localGamer);
                StartGame();
            }

            this.NetSession.Update();
            localGamer.ProcessIncomingData(this.NetReader, this.MessageReader);

            base.Update(gameTime);
        }

        protected abstract bool AllGamersReady();
        protected abstract Message GetStartGameMessage();
        private void SendStartGameMessage(Message message, LocalNetworkGamer localGamer)
        {
            message.WriteMessage(this.NetWriter, localGamer, SendDataOptions.Reliable);
        }
        protected abstract void StartGame();
    }
}
#endif